﻿/* Filename: Attack.as 
 *
 * Description: This class creates Attacks that
 * for Demons.as to use.  This class extends the
 * ElementType class to give the attack an element
 * type that alters its effects.
 *
 * Created By: Tyler Samson
 * Feb 2008
 * @ Sierra Nevada College
 */
class Attack extends ElementType {
	private static var _rangeTypes:EnumeratedType =
					new EnumeratedType("range", "melee");
	public static var _accuracyTypes:EnumeratedType =
					new EnumeratedType("miss", "hit", "critical");
	private static var _criticalChance:Number = 5;		// percentage chance that this attack is a critical hit
	
	private var _attackName:String;		// name of the attack
	private var _baseDamage:Number;		// lowest amount of damage this attack can deliver
	private var _randomDamage:Number;	// highest additional damage this attack can deliver
	private var _accuracy:Number;		// percentage chance that this attack will land successfully
	private var _rangeType:Number;
	
	public function Attack(p_attackName:String, p_baseDamage:Number,
						   p_randomDamage:Number, p_accuracy:Number,
						   p_elementType:String, p_rangeType:String) {
		
		super(p_elementType);	// Set the element type of this attack
		_attackName = p_attackName;
		_baseDamage = p_baseDamage;
		_randomDamage = p_randomDamage;
		_accuracy = p_accuracy;
		_rangeType = _rangeTypes[p_rangeType];
	}
	
	// Return the name of this attack
	public function getName():String {
		return _attackName;
	}
	
	// Return a damage amount = baseDamage plus random amount from 1 to randomDamage
	public function getDamage():Number {
		return -(_baseDamage + Math.ceil(Math.random()*_randomDamage));
	}
	
	// Return true if the attack's range type matches the parameter
	public function hasRangeType(p_rangeType:String):Boolean {
		var returnValue:Boolean = false;
		
		if (_rangeType == _rangeTypes[p_rangeType]) {
			returnValue = true;
		}
		
		return returnValue;
	}
	
	// Returns the name of the attack's effect type for printing
	public function getRangeType():String {
		return _rangeTypes[_rangeType];
	}
	
	public function isHitSuccessful():Number {
		var _hitSuccess:Number = _accuracyTypes["miss"];
		var _hitNumber:Number = Math.round(Math.random()*100);	// get random number to determine if the attack was successful
		
		if (_hitNumber <= _criticalChance) {
			_hitSuccess = _accuracyTypes["critical"];
		} else if (_hitNumber <= _accuracy) {
			_hitSuccess = _accuracyTypes["hit"];
		}
		
		return _hitSuccess;
	}
}